package com.classify.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.classify.system.domain.SgClassifyPO;
import com.classify.system.domain.SgClassifyVO;
import com.shopgoods.common.core.domain.R;
import com.shopgoods.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.shopgoods.common.log.annotation.Log;
import com.shopgoods.common.log.enums.BusinessType;
import com.shopgoods.common.security.annotation.RequiresPermissions;
import com.classify.system.domain.SgClassify;
import com.classify.system.service.ISgClassifyService;
import com.shopgoods.common.core.web.controller.BaseController;
import com.shopgoods.common.core.web.domain.AjaxResult;
import com.shopgoods.common.core.utils.poi.ExcelUtil;
import com.shopgoods.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品分类Controller
 *
 * @author shopgoods
 * @date 2024-11-14
 */
@RestController
@RequestMapping("/classify")
public class SgClassifyController extends BaseController
{
    @Autowired
    private ISgClassifyService sgClassifyService;

    /**
     * 查询商品分类列表
     */
    @RequiresPermissions("system:classify:list")
    @GetMapping("/list")
    public TableDataInfo list(SgClassify sgClassify)
    {
        startPage();
        List<SgClassify> list = sgClassifyService.selectSgClassifyList(sgClassify);
        return getDataTable(list);
    }


    /**
     * 查询商品分类列表
     */
    @GetMapping("/getNextList/{classifyId}")
    public TableDataInfo getNextList(@PathVariable("classifyId") Integer classifyId)
    {
        startPage();
        List<SgClassify> list = sgClassifyService.getNextList(classifyId);
        return getDataTable(list);
    }



    /**
     * 查询商品分类列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect()
    {
        List<SgClassify> list = sgClassifyService.treeselect();
        return AjaxResult.success(list);
    }

    /**
     * 查询下一级商品分类列表
     */
    @GetMapping("/nextlist")
    public TableDataInfo nextlist(Integer parentCategoryId)
    {
        startPage();
        List<SgClassify> list = sgClassifyService.selectSgClassifyNextList(parentCategoryId);
        return getDataTable(list);
    }



    /**
     *
     *
     * 导出商品分类列表
     */
    @RequiresPermissions("system:classify:export")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SgClassify sgClassify)
    {
        List<SgClassify> list = sgClassifyService.selectSgClassifyList(sgClassify);
        ExcelUtil<SgClassify> util = new ExcelUtil<SgClassify>(SgClassify.class);
        util.exportExcel(response, list, "商品分类数据");
    }

    /**
     * 获取商品分类详细信息
     */
    @RequiresPermissions("system:classify:query")
    @GetMapping(value = "/{classifyId}")
    public AjaxResult getInfo(@PathVariable("classifyId") Integer classifyId)
    {
        return success(sgClassifyService.selectSgClassifyByClassifyId(classifyId));
    }

    /**
     * 新增商品分类
     */
    @RequiresPermissions("system:classify:add")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PutMapping("add")
    public AjaxResult add(@RequestBody SgClassifyPO sgClassify)
    {
        return toAjax(sgClassifyService.insertSgClassify(sgClassify));
    }

    /**
     * 修改商品分类
     */
    @RequiresPermissions("system:classify:edit")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SgClassifyVO sgClassify)
    {
        return toAjax(sgClassifyService.updateSgClassify(sgClassify));
    }

    /**
     * 删除商品分类
     */
    @RequiresPermissions("system:classify:remove")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{classifyIds}")
    public AjaxResult remove(@PathVariable Integer[] classifyIds)
    {
        return toAjax(sgClassifyService.deleteSgClassifyByClassifyIds(classifyIds));
    }


    /**
     * 图片上传
     */
    @PostMapping("/avatar")
    public R<SysFile> avatar(MultipartFile file) {
        return sgClassifyService.avatar(file);
    }


    /**
     * 修改是否启用
     */
    @PutMapping("/updateClassifyisInNavbarData")
    public AjaxResult updateClassifyisInNavbarData(@RequestBody SgClassify sgClassify)
    {
        return toAjax(sgClassifyService.updateClassifyisInNavbarData(sgClassify));
    }


    /**
     * 修改是否显示
     */
    @PutMapping("/updateClassifyisDisplayedData")
    public AjaxResult updateClassifyisDisplayedData(@RequestBody SgClassify sgClassify)
    {
        return toAjax(sgClassifyService.updateClassifyisDisplayedData(sgClassify));
    }

}
